#ifndef TITLEBAR_H
#define TITLEBAR_H

#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QFrame>
#include <QScrollArea>
#include <QAction>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QPainter>

#include <QQueue>
#include <QTimer>

#include <clickedlabel.h>

//接口
/*
class TitleBar
{
signals:
    void returnSignal();返回
    void userUiSignal();点击用户头像
    void userNameSignal();点击用户名
    *void userInfoSignal();编辑用户信息
    void userExitSignal();用户退出的信号
    void dynamicSignal();点击动态
    void concernSignal();点击关注
    void followSignal();点击粉丝
    void minSignal();最小化
    void maxSignal();最大化
    void windowingSignal();窗口化
    void closeSignal();关闭
    void searchSignal(const QString& text);向上层发出要搜索的信号
    void mailSignal();邮箱
    void settingSignal();点击设置
public:
    void SetSearchToolTip(const QStringList& str);设置搜索框定时变化的ToolTip文本
    void onMainWindowResize(QSize size);当主窗口变化时随之变化
    SearchListWidget* GetSearchListW(){return searchlistw;}获取内部的SearchListWidget*，用于添加历史记录、热搜和相关搜索
    void SetDynamicNum(qint32 num);设置动态数
    void SetConcornNum(qint32 num);设置关注数
    void SetFollowNum(qint32 num);设置粉丝数
};

 */





namespace Ui {
class TitleBar;
}

class SearchListWidget;
class UserInfoWidget;

class TitleBar : public QWidget
{
    Q_OBJECT

public:
    explicit TitleBar(QWidget *parent = nullptr);
    ~TitleBar();
    void SetPlaceholderText(const QStringList& str);
    SearchListWidget* GetSearchListW(){return searchlistw;}
    void SetDynamicNum(qint32 num);
    void SetConcornNum(qint32 num);
    void SetFollowNum(qint32 num);
    void SetUserName(const QString&username){usernamebtn->setText(username);}
    void SetHeadShot(const QString&filepath){headshotbtn->setIcon(QIcon(filepath));headshotbtn->setIconSize(QSize(40,40));}

signals:
    void returnSignal();
    void userUiSignal();
    void userNameSignal();
    void userInfoSignal();
    void userExitSignal();
    void dynamicSignal();
    void concernSignal();
    void followSignal();
    void minSignal();
    void maxSignal();
    void windowingSignal();
    void closeSignal();
    void searchSignal(const QString& text);
    void mailSignal();
    void settingSignal();

public slots:
    void onMainWindowResize(QSize size);

private slots:
    void onPlaceholderText();
protected:
    void paintEvent(QPaintEvent *e) override;
    void mousePressEvent(QMouseEvent* e) override;
    void mouseReleaseEvent(QMouseEvent* e) override;
    void mouseMoveEvent(QMouseEvent* e) override;
    void keyPressEvent(QKeyEvent* e) override;
    void onTimer();
private:

    class LineEdit : public QLineEdit
    {
    public:
        LineEdit(QWidget *parent = nullptr) : QLineEdit(parent) {}
        void focusInEvent(QFocusEvent *e) override;
        void focusOutEvent(QFocusEvent *e) override;
        SearchListWidget *searchlistw = nullptr;

        QAction*searchact,*clearact;
    };

    QWidget *m;
    QPushButton *tagbtn, *headshotbtn, *mailbtn, *settingbtn, *minbtn, *maxbtn;
    QPushButton *windowingbtn, *closebtn, *usernamebtn, *returnbtn, *voicebtn;
    LineEdit *searchline;
    QHBoxLayout *hlayout;
    QFrame *frame;
    SearchListWidget *searchlistw;
    UserInfoWidget*userinfow;

    QStringList strlist;
    QTimer*timer;
    qint32 strlistpos=0;
    int pos=0;
    bool isdrag = false;
    QPointF startpoint;

private:
    Ui::TitleBar *ui;
};

class AdaptiveLabel:public ClickedLabel
{
    Q_OBJECT
public:
    AdaptiveLabel(QWidget*parent=nullptr);
    void setAdaptiveText(const QString&text);
    const QString adaptiveText()const;
    const QString text()const;

protected:
    void mousePressEvent(QMouseEvent*e)override;
private:
    QString realtext;
};

class SearchListWidget : public QWidget
{
    Q_OBJECT

public:
    explicit SearchListWidget(QWidget *parent = nullptr);

    void UpDataHistoryFront();
    void UpDataHistoryBack();
    void UpDataHot();
    void UpDataSearch();

    QPushButton* setHotStyle(QPushButton *button);

signals:
    void searchSignal(const QString&);
public slots:
    void addHistory(const QStringList &strlist);
    void addHot(const QStringList &strlist);
    void addSearch(const QStringList &strlist);
    void readdHistory(AdaptiveLabel*label);
    void clearHistory();
    void clearSearch();
    void turnToNormal();
    void turnToSearching();
    void show(){area->show();QWidget::show();}
    void hide(){area->hide();QWidget::hide();}
    void setGeometry(const QRect&rect){QWidget::setGeometry(rect);area->setGeometry(rect);}

private:
    bool issearching=false;
    QScrollArea *area;
    QWidget *normalw, *searchingw;
    QVBoxLayout *mainlayout1, *mainlayout2;
    QHBoxLayout *hlayout;
    QLabel *searchhistory, *hotsearch, *guesssearch;
    QPushButton*deletebtn;

    QQueue<AdaptiveLabel*> historyinsert;
    QQueue<AdaptiveLabel*> historyready;
    QQueue<QHBoxLayout*> hlayoutsinsert;
    QQueue<QHBoxLayout*> hotsinsert;
    QQueue<QHBoxLayout*> hotsready;
    QQueue<QPushButton*> searchsinsert;
    QQueue<QPushButton*> searchsready;
};

class MyLabel : public QLabel
{
    Q_OBJECT
public:
    MyLabel(QWidget*p=nullptr):QLabel(p){}
    void setText(qint32 num,const QString&t);
signals:
    void clickLabel();
protected:
    void mousePressEvent(QMouseEvent*e);
private:
};

class UserInfoWidget:public QWidget
{
    Q_OBJECT
public:
    UserInfoWidget(QWidget*parent=nullptr);
    void SetDynamicNum(qint32 num);
    void SetConcornNum(qint32 num);
    void SetFollowNum(qint32 num);

signals:
    void clickDynamic();
    void clickConcern();
    void clickFollow();
    void clickUserInfo();
    void clickUserExit();

protected:
    void paintEvent(QPaintEvent*e)override;
    void focusOutEvent(QFocusEvent *e)override;
private:



    QHBoxLayout*hlayout;
    QVBoxLayout*vlayout;
    QVector<MyLabel*>labels;
    QVector<QPushButton*>buttons;
};

#endif // TITLEBAR_H
